﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

namespace yq.IBase
{
    /// <summary>
    /// 拼接普通sql语句
    /// DbHelper的封装类
    /// </summary>
    public class SQLHelper
    {
        private DbHelper cn;

        private DbCommand cmd;

        private CommandType type;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_cn"></param>
        /// <param name="type">执行类型 存储过程orSQL语句</param>
        public SQLHelper(DbHelper _cn, CommandType _type = CommandType.Text)
        {
            cn = _cn;
            type = _type;
            if (type == CommandType.Text)
            {
                cmd = cn.GetSqlStringCommond("");
            }
            else
            {
                cmd = cn.GetStoredProcCommond("");
            }
        }


        public void Clear()
        {
            if (type == CommandType.Text)
            {
                cmd = cn.GetSqlStringCommond("");
            }
            else
            {
                cmd = cn.GetStoredProcCommond("");
            }
            cmd.Parameters.Clear();
        }

        /// <summary>
        /// 输出参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="dbType"></param>
        /// <param name="size"></param>
        public void AddOutParameter(string parameterName, DbType dbType, int size)
        {
            cn.AddOutParameter(cmd, parameterName, dbType, size);
        }

        /// <summary>
        /// 输入参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="dbType"></param>
        /// <param name="value"></param>
        public void AddInParameter(string parameterName, DbType dbType, object value)
        {
            cn.AddInParameter(cmd, parameterName, dbType, value);
        }

        /// <summary>
        /// 返回值
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="dbType"></param>
        public void AddReturnParameter(string parameterName, DbType dbType)
        {
            cn.AddReturnParameter(cmd, parameterName, dbType);
        }

        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataSet ExecuteDataSet(string sql)
        {
            cmd.CommandText = sql;
            return cn.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable ExecuteDataTable(string sql)
        {
            cmd.CommandText = sql;
            return cn.ExecuteDataTable(cmd);
        }

        /// <summary>
        ///  查询返回DbDataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DbDataReader ExecuteReader(string sql)
        {
            cmd.CommandText = sql;
            return cn.ExecuteReader(cmd);
        }

        /// <summary>
        /// 执行SQL返回影响行
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string sql)
        {
            cmd.CommandText = sql;
            return cn.ExecuteNonQuery(cmd);
        }

        /// <summary>
        /// 执行SQL返回一个结果
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public object ExecuteScalar(string sql)
        {
            cmd.CommandText = sql;
            return cn.ExecuteScalar(cmd);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="currentPageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public DataTable ExecutePaging(string sql,string orderby, int currentPageIndex, int pageSize, out int pageCount, out int count)
        {
            return cn.Paging(sql, orderby, currentPageIndex, pageSize, out pageCount, out count);
        }

    }
}
